﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;


namespace CommonLibCore
{
    public class HttpJsonOper
    {
        public string JsonHelper<T>(T t, string url)where T :new()
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                // Set the Method property of the request to POST.
                request.Method = "POST";
                string postData = JsonConvert.SerializeObject(t);
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                request.ContentType = "application/json";

                request.ContentLength = byteArray.Length;

                Stream dataStream = request.GetRequestStream();


                dataStream.Write(byteArray, 0, byteArray.Length);

                dataStream.Close();

                WebResponse response = request.GetResponse();

                dataStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(dataStream);

                string responseFromServer = reader.ReadToEnd().Replace("\\", "");

                reader.Close();
                dataStream.Close();
                response.Close();
                if (!string.IsNullOrEmpty(responseFromServer))
                {
                   // res = (T)JsonConvert.DeserializeObject(responseFromServer, typeof(T));
                    return responseFromServer;
                }
                else
                {
                    return null;
                }              
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string JsonHelperStr(string str, string url)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                // Set the Method property of the request to POST.
                request.Method = "POST";
                //string postData = JsonConvert.SerializeObject(t);
                byte[] byteArray = Encoding.UTF8.GetBytes(str);

                request.ContentType = "application/json";

                request.ContentLength = byteArray.Length;

                Stream dataStream = request.GetRequestStream();


                dataStream.Write(byteArray, 0, byteArray.Length);

                dataStream.Close();

                WebResponse response = request.GetResponse();

                dataStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(dataStream);

                string responseFromServer = reader.ReadToEnd().Replace("\\", "");

                reader.Close();
                dataStream.Close();
                response.Close();
                if (!string.IsNullOrEmpty(responseFromServer))
                {
                    // res = (T)JsonConvert.DeserializeObject(responseFromServer, typeof(T));
                    return responseFromServer;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
